/**
 * api 相关的回调事件的注册
 */

// import type { IObject } from "@/types/customs";

const handleFail = ({ msg }: HttpResult.ResponseData<unknown>) => {
  uni.showToast({ title: msg, icon: 'none' })
}

const handleTokenExpired = () => {
  uni.removeStorageSync('token')
  uni.removeStorageSync('userInfo')
  // uni.clearStorage();

  uni.reLaunch({
    url: '/pages/login',
  })
}

export enum API_EVENTS {
  EVENT_201_FAIL = 'error:201:fail',
  EVENT_202_TOKEN_EXPIRED = 'error:202:tokenExpired',
  LOGIN_OUT = 'logout',
}

export const registerApiHanderEvents = () => {
  uni.$on(API_EVENTS.EVENT_201_FAIL, handleFail)
  uni.$on(API_EVENTS.EVENT_202_TOKEN_EXPIRED, handleTokenExpired)
  uni.$on(API_EVENTS.LOGIN_OUT, handleTokenExpired)
}
